package com.sqx.modules.course.controller.app;

import cn.hutool.core.util.RandomUtil;
import com.sqx.common.utils.Result;
import com.sqx.modules.course.util.FfmpegUtil;
import io.swagger.annotations.ApiParam;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.FrameGrabber;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ws.schild.jave.info.MultimediaInfo;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

@RestController
@RequestMapping("/app/video")
public class AppVideoController {


    /**
     * 获取视频信息
     * @param url
     * @param type
     * @return
     * @throws MalformedURLException
     */
    @GetMapping("/getVideoInfo")
    public Result getVideoInfo(@ApiParam("url或者本地路径") String url,@ApiParam("类型：1.url2.本地路径") int type) throws MalformedURLException {
        MultimediaInfo videoInfo = null;
        if(type==1){
            videoInfo = FfmpegUtil.getVideoInfo(url,type);
        }else if (type==2){
            videoInfo = FfmpegUtil.getVideoInfo(url,type);
        }
        System.out.println("videoInfo = " + videoInfo);
        return Result.success().put("data",videoInfo);
    }

    /**
     *
     * @param path 剪辑文件路径
     * @param toPath 剪辑后文件夹路径
     * @param startTime  00:00:00.000
     * @param endTime
     * @return
     */
    @GetMapping("/shearVideo")
    public Result shearVideo(String path,String toPath,@ApiParam("剪辑开始时间") String startTime,@ApiParam("剪辑持续时间") String endTime){
        toPath = toPath + "\\" + RandomUtil.randomString(6) + ".mp4";
        System.out.println(toPath);
        FfmpegUtil.cutAv(path,toPath,startTime,endTime);
        return Result.success("剪切视频成功");
    }

    /**
     * 合并两个视频
     * @param firstFragmentPath 第一个视频路径
     * @param secondFragmentPath 第二个视频路径
     * @param targetPath 合并后视频路径
     * @return  竖屏和横屏视频合并不了,根本原因是分辨率不一致，合并需保证分辨率一致
     * 某个视频编码设置不支持也会导致合并失败
     * 分辨率、编码、帧率、音频编码、音频采样率、音频通道数、音频比特率、音频采样
     * 调整分辨率、编码、帧率后合并视频还是会出现编码格式错误的问题
     * ffmpeg -i 1.mp4 -i 2.mp4 -c:v copy -c:a copy -shortest 3.mp4
     *
     * 画面的比例不对会导致合并视频失败
     * 视频编码格式不一致会导致合并视频失败
     * 帧率不一致会导致合并视频失败
     *
     */
    @GetMapping("/merageVideo")
    public Result merageVideo(String firstFragmentPath, String secondFragmentPath,String targetPath){
        targetPath = targetPath + "\\" + RandomUtil.randomString(6) + ".mp4";
        //判断视频的分辨率是否一致，不一致则调整分辨率
        return Result.success("合并视频成功").put("data",FfmpegUtil.mergeAv(firstFragmentPath, secondFragmentPath, targetPath));
    }

    /**
     * 提取视频原声
     * @return
     */
    @GetMapping("/extractAudio")
    public Result extractAudio(@ApiParam("本地路径或者rul") String sourcePath, @ApiParam("本地存储路径") String targetPath, String taskId){
        FfmpegUtil.getAudio(sourcePath,targetPath,taskId);
        return Result.success("提取视频原声成功");
    }

    /**
     * 给视频添加水印
     * @param filePath
     * @param text
     * @return
     */
    @GetMapping("/codecToMp4WithText")
    public Result codecToMp4WithText(String filePath, String text){
        FfmpegUtil.codecToMp4WithText(filePath,text);
        return Result.success("添加水印成功");
    }

    /**
     * 调整视频清晰度
     * @param inputPath 输入视频路径
     * @param outputPath 输出视频路径
     * @param targetWidth 目标宽度
     * @param targetHeight 目标高度
     * @return
     */
    @GetMapping("/adjustVideoQuality")
    public Result adjustVideoQuality(String inputPath, String outputPath, int targetWidth, int targetHeight,Integer bitRate) throws FFmpegFrameRecorder.Exception, FrameGrabber.Exception {
        try {
            FfmpegUtil.modifyResolution(inputPath, outputPath, targetWidth, targetHeight,bitRate);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return Result.success("调整视频清晰度成功");
    }


    @GetMapping("/addAPictureWatermark")
    public Result addAPictureWatermark(String inputVideo,String watermarkImage,String outputVideo){
        FfmpegUtil.addAPictureWatermark(inputVideo, watermarkImage, outputVideo);
        return Result.success("添加水印成功");
    }

    @GetMapping("/addAWriteWatermark")
    public Result addAWriteWatermark(String inputVideo,String text,String outputVideo){
        String url = FfmpegUtil.addAWriteWatermark(inputVideo, text, outputVideo);
        return Result.success("添加水印成功").put("data",url);
    }



}
